package edu.lnu.ums.dal.impl;

import edu.lnu.ums.dal.IUserDAO;
import edu.lnu.ums.dal.factories.FactoriesCache;
import edu.lnu.ums.entities.security.User;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;

@Stateless
public class UserDAO implements IUserDAO {
    public UserDAO() {
    }

    @Override
    public User getByName(String login) {
        EntityManager em = FactoriesCache.getInstance().getDefaultFactory().createEntityManager();

        String query = "from User where login ='" + login + "'";
        List<User> users = em.createQuery(query).getResultList();

        em.close();
        if (users.size() == 0) {
            return null;
        }

        if (users.size() == 1) {
            return users.get(0);
        }

        throw new IllegalStateException(
                "There can not be more than one user with same login!");


    }

    @Override
    public void addUser(User user) {
        EntityManager em = FactoriesCache.getInstance().getDefaultFactory().createEntityManager();
        user.setActive(true);
        em.getTransaction().begin();
        em.persist(user);
        em.getTransaction().commit();
        em.close();
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<User> getAllUsers() {
        EntityManager em = FactoriesCache.getInstance().getDefaultFactory().createEntityManager();
        String query = "from User order by login";
        return em.createQuery(query).getResultList();
    }

    @Override
    public void editUser(User user) {
        EntityManager em = FactoriesCache.getInstance().getDefaultFactory().createEntityManager();
        em.getTransaction().begin();
        em.merge(user);
        em.getTransaction().commit();
        em.close();
    }

    @Override
    public void removeUser(User user) {
        EntityManager em = FactoriesCache.getInstance().getDefaultFactory().createEntityManager();
        em.getTransaction().begin();
        User merged = (User) em.merge(user);
        em.remove(merged);
        em.getTransaction().commit();
        em.close();
    }

    @SuppressWarnings("unchecked")
    @Override
    public UserValidationResult validateUser(User user, String oldUserName) {
        EntityManager em = FactoriesCache.getInstance().getDefaultFactory().createEntityManager();

        if ("".equals(user.getName())) {
            return UserValidationResult.BAD_LOGIN;
        }

        if ("".equals(user.getPassword())) {
            return UserValidationResult.BAD_PASSWORD;
        }

        String query = "from User where login ='" + user.getName() + "'";
        List<User> users = em.createQuery(query).getResultList();
        em.close();
        
        if (users.size() == 0) {
            return UserValidationResult.OK;
        } else {
            if (oldUserName.equals(user.getName())) {
                return UserValidationResult.EXISTING_LOGIN;
            } else {
                return UserValidationResult.EXISTING_EDIT_LOGIN;
            }
        }
    }
}
